﻿using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using NUnit.Framework;
using Spring.Context;
using Spring.Context.Support;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Application.MainBoundedContext.PurchaseModule;
using Newtonsoft.Json;
namespace Huirui.Cavan.TestAppService
{
    public class TestPurchaseOrderAppService
    {
        private IApplicationContext context;
        private IDepartmentAppService DepartmentAppService;
        private IEmployeeAppService EmployeeAppService;
        private IAccountAppService AccountAppService;
        private IStockInOutTypeAppService StockInOutTypeAppService;
        private ISupplierAppService SupplierAppService;
        private IStockInOutAppService StockInOutAppService;
        private IWareHouseAppService WareHouseAppService;
        private IStockAppService StockAppService;
        private IPurchaseOrderAppService PurchaseOrderAppService;

        [SetUp]
        public void SetUp()
        {
            context = ContextRegistry.GetContext();
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
        }

        private void GetAppService()
        {
            this.DepartmentAppService = context.GetObject(SystemModuleConst.DEPARTMENTAPPSERVICE) as IDepartmentAppService;
            this.EmployeeAppService = context.GetObject(SystemModuleConst.EMPLOYEEAPPSERVICE) as IEmployeeAppService;
            this.AccountAppService = context.GetObject(SystemModuleConst.ACCOUNTAPPSERVICE) as IAccountAppService;
            this.StockInOutTypeAppService = context.GetObject(StockModuleConst.STOCKINOUTTYPEAPPSERVICE) as IStockInOutTypeAppService;
            this.SupplierAppService = context.GetObject(SystemModuleConst.SUPPLIERAPPSERVICE) as ISupplierAppService;
            this.StockInOutAppService = context.GetObject(StockModuleConst.STOCKINOUTAPPSERVICE) as IStockInOutAppService;
            this.WareHouseAppService = context.GetObject(StockModuleConst.WAREHOUSEAPPSERVICE) as IWareHouseAppService;
            this.StockAppService = context.GetObject(StockModuleConst.STOCKAPPSERVICE) as IStockAppService;
            this.PurchaseOrderAppService = context.GetObject(PurchaseModuleConst.PURCHASEORDERAPPSERVICE) as IPurchaseOrderAppService;
        }

        [Test]
        public void TestAddOrUpdate()
        {
            GetAppService();
            Employee employee = this.EmployeeAppService.GetById("001_e11");
            Supplier supplier = this.SupplierAppService.GetById("001_gys001");
            WareHouse wareHouse = this.WareHouseAppService.GetById("001_wh_001");
            Stock stock = this.StockAppService.GetById("001_ch001");

            PurchaseOrderDetail purchaseOrderDetail1 = new PurchaseOrderDetail()
            {
                Description = "test",
                DiscountAmount = 10,
                DiscountRate = (decimal)0.17,
                Number = 10,
                PurchaseAmount = 100,
                Stock = stock,
                TaxAmount = 100,
                TaxRate = (decimal)0.1,
                UnitCost = 10,
                WareHouse = wareHouse,
                WholeAmount = 100
            };
            IList<PurchaseOrderDetail> details = new List<PurchaseOrderDetail>();
            details.Add(purchaseOrderDetail1);

            PurchaseOrder purchaseOrder = new PurchaseOrder()
            {
                CheckEmployee = null,
                Currency = "RMB",
                CurrencyRate = 1,
                DeliveryAddress = "test",
                DeliveryDate = DateTime.Now,
                Project = null,
                PurchaseDate = DateTime.Now,
                PurchaseEmployee = employee,
                Remark = "test",
                Supplier = supplier,
                SupplierInvoiceNumber = "1000",
                Contact = "001_e11",
                ContactTelephone = "12345678",
                PurchaseOrderDetails = details
            };

            BoolGenericResult<PurchaseOrder> result = this.PurchaseOrderAppService.AddOrUpdate(purchaseOrder);

            Assert.IsNotEmpty(result.Object.Id);
        }

        [Test]
        public void TestJson()
        {
            GetAppService();
            Stock stock = new Stock();
            stock.Id = "100";

            PurchaseOrder purchaseOrder = new PurchaseOrder();
            purchaseOrder.Id = "1";
            PurchaseOrderDetail purchaseOrderDetail1 = new PurchaseOrderDetail()
            {
                Id = 1,
                Number = 10,
                Stock = stock
            };
            IList<PurchaseOrderDetail> details = new List<PurchaseOrderDetail>();
            details.Add(purchaseOrderDetail1);
            purchaseOrder.PurchaseOrderDetails = details;

            string json = JsonConvert.SerializeObject(purchaseOrder);

            Assert.Ignore();
        }
    }
}
